package com.hxkcloud.nurse.views;



import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * 
 * @Package: com.yipong.app.view
 * @ClassName: SwipeLayout.java
 * @Description:侧滑菜单
 * @auther: allen_lee
 * @Date: 2016-2-22 下午6:30:41
 * 
 */

public class SwipeLayout extends FrameLayout{
	private State mState = State.CLOSE;

	public enum State {
		CLOSE, OPEN, DRAGGING
	}

	private boolean isCanSmooth;
	
	public boolean getCanSmooth(){
		return isCanSmooth;
	}
	
	public boolean setCanSmooth(boolean canSmooth) {
		this.isCanSmooth = canSmooth;
		return this.isCanSmooth;

	}

	// 点击事件的回调监听
	public interface OnClickChildListener {
		void onClickChildMain();

		void onClickChildBack(int childPosition);

		void onJustCloseAll();
	}
	
	//禁止滑动监听
	public interface OnStopListener {
		boolean onStop(SwipeLayout layout);

	}
	
	private OnStopListener mOnStopListener;


	public OnStopListener getmOnStopListener() {
		return mOnStopListener;
	}

	public void setmOnStopListener(
			OnStopListener mOnStopListener) {
		this.mOnStopListener = mOnStopListener;
	}
	
	// 滑动状态监听
	public interface OnDragStateChangeListener {
		void onClose(SwipeLayout layout);

		void onOpen(SwipeLayout layout);

		void onDragging(SwipeLayout layout);

		void onStartOpen(SwipeLayout layout);

		void onStartClose(SwipeLayout layout);

	}

	private OnDragStateChangeListener mOnDragStateChangeListener;

	public State getmState() {
		return mState;
	}

	public void setmState(State mState) {
		this.mState = mState;
	}

	public OnDragStateChangeListener getmOnDragStateChangeListener() {
		return mOnDragStateChangeListener;
	}

	public void setmOnDragStateChangeListener(
			OnDragStateChangeListener mOnDragStateChangeListener) {
		this.mOnDragStateChangeListener = mOnDragStateChangeListener;
	}

	// 提供信息，接收事件
	private ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() {
		@Override
		public boolean tryCaptureView(View child, int pointerId) {
			return true;
		}

		@Override
		public int clampViewPositionHorizontal(View child, int left, int dx) {
			// 通知父布局不要拦截touch事件，用于解决滑动冲突
			getParent().requestDisallowInterceptTouchEvent(true);
			if (child == mFrontView) {
				if (left < -mDragRange) {
					left = -mDragRange;
				} else if (left > 0) {
					left = 0;
				}
			} else if (child == mBackView) {
				if (left < mFrontWidth - mDragRange) {
					left = mFrontWidth - mDragRange;
				} else if (left > mFrontWidth) {
					left = mFrontWidth;
				}
			}
			return left;
		}

		@Override
		public void onViewPositionChanged(View changedView, int left, int top,
				int dx, int dy) {
			if (changedView == mFrontView) {
				mBackView.offsetLeftAndRight(dx);
			} else if (changedView == mBackView) {
				mFrontView.offsetLeftAndRight(dx);
			}
			// if (left == 0) {
			// getParent().requestDisallowInterceptTouchEvent(false);
			// }
			// 根据frontView的left值判断状态
			dispatchDragState(mFrontView.getLeft());
			
			invalidate();
		}

		@Override
		public int getViewHorizontalDragRange(View child) {
			return mDragRange;
		}

		// xvel>0，向x轴正方向。xvel<0,向x轴负方向
		@Override
		public void onViewReleased(View releasedChild, float xvel, float yvel) {
			if (xvel == 0 && mFrontView.getLeft() < -mDragRange * 0.5f) {
				// 打开
				open();
			} else if (xvel < 0) {
				// 打开
				open();
			} else {
				// 关闭
				close();

			}
			isCaptureView = false;
			getParent().requestDisallowInterceptTouchEvent(false);
			if (mStopPulltorefreshScrollerListener != null) {
				mStopPulltorefreshScrollerListener.setCanScroll(true);
			}
		}
	};

	private void dispatchDragState(int left) {
		State preState = mState;
		mState = updateState(left);
		if (mOnDragStateChangeListener != null) {
			if (mState != preState) {
				mOnDragStateChangeListener.onDragging(this);
				if (mState == State.OPEN) {
					mOnDragStateChangeListener.onOpen(this);
				} else if (mState == State.CLOSE) {
					mOnDragStateChangeListener.onClose(this);
				} else if (mState == State.DRAGGING) {
					if (preState == State.CLOSE) {
						mOnDragStateChangeListener.onStartOpen(this);
					} else if (preState == State.OPEN) {
						mOnDragStateChangeListener.onStartClose(this);
					}
				}
			}
		}
	}
	
	

	private State updateState(int left) {
		if (left == 0) {
			// 关闭
			return State.CLOSE;
		} else if (left == -mDragRange) {
			// 打开
			return State.OPEN;
		} else {
			// 拖拽中
			return State.DRAGGING;
		}
	}

	private ViewDragHelper mDragHelper;
	private ViewGroup mBackView;
	private ViewGroup mFrontView;
	private int mDragRange;
	private int mHeight;
	private int mFrontWidth;
	private float firstTouchY;
	private float firstTouchX;
	private boolean isCaptureView;

	public SwipeLayout(Context context) {
		this(context, null);
	}

	public SwipeLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		mDragHelper = ViewDragHelper.create(this, mCallback);
	}

	// 让ViewDragHelper处理 拦截事件
	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if(!mOnStopListener.onStop(this)){
				mDragHelper.cancel();
			    return false;
			}else{
			}
			firstTouchY = event.getY();
			firstTouchX = event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			// isCaptureView为 让mDragHelper判断是否处理View的滑动事件
			if (Math.abs(event.getX() - firstTouchX) > 4 * (Math.abs(event
					.getY() - firstTouchY))) {
				if (mStopPulltorefreshScrollerListener != null) {
					mStopPulltorefreshScrollerListener.setCanScroll(false);
				}
				isCaptureView = true;
			} else {
				isCaptureView = false;
			}
			break;
		case MotionEvent.ACTION_UP:

			break;
		}
		return mDragHelper.shouldInterceptTouchEvent(event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		mDragHelper.processTouchEvent(event);
		return true;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mBackView = (ViewGroup) getChildAt(0);
		mFrontView = (ViewGroup) getChildAt(1);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mDragRange = mBackView.getMeasuredWidth();
		mHeight = mFrontView.getMeasuredHeight();
		mFrontWidth = mFrontView.getMeasuredWidth();
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		mBackView.layout(mFrontWidth, 0, mDragRange + mFrontWidth, mHeight);

	}

	public void open() {
		open(true);
	}

	public void close() {
		close(true);
	}

	// 摆放两个view的位置
	private void layoutContent(boolean isOpen) {
		Rect frontRect = computeFrontRect(isOpen);
		Rect backRect = computeBackRectWithFront(frontRect);
		mFrontView.layout(frontRect.left, frontRect.top, frontRect.right,
				frontRect.bottom);
		mBackView.layout(backRect.left, backRect.top, backRect.right,
				backRect.bottom);
	}

	private Rect computeBackRectWithFront(Rect frontRect) {
		return new Rect(frontRect.right, 0, frontRect.right + mDragRange,
				mHeight);
	}

	// 计算Rect
	private Rect computeFrontRect(boolean isOpen) {
		int left = 0;
		if (isOpen) {
			// 打开情况下
			left = -mDragRange;
		}
		return new Rect(left, 0, left + mFrontWidth, mHeight);
	}

	public void open(boolean isSmooth) {
		int finalLeft = -mDragRange;

		if (isSmooth) {
			mDragHelper.smoothSlideViewTo(mFrontView, finalLeft, 0);
			invalidate();
		} else {
			layoutContent(true);
		}

	}

	public void close(boolean isSmooth) {
		int finalLeft = 0;
		if (isSmooth) {
			mStopPulltorefreshScrollerListener.setCanScroll(true);
			mDragHelper.smoothSlideViewTo(mFrontView, finalLeft, 0);
			invalidate();
		} else {
			layoutContent(false);
		}
	}

	@Override
	public void computeScroll() {
		super.computeScroll();
		if (mDragHelper.continueSettling(true)) {
			ViewCompat.postInvalidateOnAnimation(this);
		}
	}

	public interface StopPulltorefreshScrollerListener {
		void setCanScroll(boolean canScoller);
	}

	private StopPulltorefreshScrollerListener mStopPulltorefreshScrollerListener;

	public void setStopPulltorefreshScrollerListener(
			StopPulltorefreshScrollerListener mStopPulltorefreshScrollerListener) {
		this.mStopPulltorefreshScrollerListener = mStopPulltorefreshScrollerListener;
	}


	

	
	
}
